﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using System.Drawing.Text;

namespace NoteSharp_RTF_Editor
{
    public partial class Form1 : Form
    {
        System.Drawing.Printing.PrintDocument prntDoc = new System.Drawing.Printing.PrintDocument();
        public Form1()
        {
            InitializeComponent();
            
        }
        private void ofd_FileOk(object sender, EventArgs e)
        {

        }

        private void Form1_Load(object sender, EventArgs e)
        {
            zoomList.DropDown.Items.Add("300%");
            zoomList.DropDown.Items.Add("200%");
            zoomList.DropDown.Items.Add("100%"); 

            sizeList.DropDown.Items.Add("8");
            sizeList.DropDown.Items.Add("12");
            sizeList.DropDown.Items.Add("16");
            sizeList.DropDown.Items.Add("24");
            sizeList.DropDown.Items.Add("36");
            sizeList.DropDown.Items.Add("72");

            colorList.DropDown.Items.Add("AliceBlue");
            colorList.DropDown.Items.Add("AntiqueWhite");
            colorList.DropDown.Items.Add("Aqua");
            colorList.DropDown.Items.Add("Aquamarine");
            colorList.DropDown.Items.Add("Azure");
            colorList.DropDown.Items.Add("Beige");
            colorList.DropDown.Items.Add("Bisque");
            colorList.DropDown.Items.Add("Black");
            colorList.DropDown.Items.Add("BlanchedAlmond");
            colorList.DropDown.Items.Add("Blue");
            colorList.DropDown.Items.Add("BlueViolet");
            colorList.DropDown.Items.Add("Brown");
            colorList.DropDown.Items.Add("BurlyWood");
            colorList.DropDown.Items.Add("CadetBlue");
            colorList.DropDown.Items.Add("Chartreuse");
            colorList.DropDown.Items.Add("Chocolate");
            colorList.DropDown.Items.Add("Coral");
            colorList.DropDown.Items.Add("CornflowerBlue");
            colorList.DropDown.Items.Add("Cornsilk");
            colorList.DropDown.Items.Add("Crismon");
            colorList.DropDown.Items.Add("Cyan");
            colorList.DropDown.Items.Add("DarkBlue");
            colorList.DropDown.Items.Add("DarkCyan");
            colorList.DropDown.Items.Add("DarkGoldenrod");
            colorList.DropDown.Items.Add("DarkGray");
            colorList.DropDown.Items.Add("DarkGreen");
            colorList.DropDown.Items.Add("DarkKhaki");
            colorList.DropDown.Items.Add("DarkMagenta");
            colorList.DropDown.Items.Add("DarkOliveGreen");
            colorList.DropDown.Items.Add("DarkOrange");
            colorList.DropDown.Items.Add("DarkOrchid");
            colorList.DropDown.Items.Add("DarkRed");
            colorList.DropDown.Items.Add("DarkSalmon");
            colorList.DropDown.Items.Add("DarkSeaGreen");
            colorList.DropDown.Items.Add("DarkSlateBlue");
            colorList.DropDown.Items.Add("DarkSlateGray");
            colorList.DropDown.Items.Add("DarkTurquoise");
            colorList.DropDown.Items.Add("DarkViolet");
            colorList.DropDown.Items.Add("DeepPink");
            colorList.DropDown.Items.Add("DeepSkyBlue");
            colorList.DropDown.Items.Add("DimGray");
            colorList.DropDown.Items.Add("DodgerBlue");
            colorList.DropDown.Items.Add("Firebrick");
            colorList.DropDown.Items.Add("FloralWhite");
            colorList.DropDown.Items.Add("ForestGreen");
            colorList.DropDown.Items.Add("Fuschia");
            colorList.DropDown.Items.Add("Gainsboro");
            colorList.DropDown.Items.Add("GhostWhite");
            colorList.DropDown.Items.Add("Gold");
            colorList.DropDown.Items.Add("Goldenrod");
            colorList.DropDown.Items.Add("Gray");
            colorList.DropDown.Items.Add("Green");
            colorList.DropDown.Items.Add("GreenYellow");
            colorList.DropDown.Items.Add("Honeydew");
            colorList.DropDown.Items.Add("HotPink");
            colorList.DropDown.Items.Add("IndianRed");
            colorList.DropDown.Items.Add("Indigo");
            colorList.DropDown.Items.Add("Ivory");
            colorList.DropDown.Items.Add("Khaki");
            colorList.DropDown.Items.Add("Lavender");
            colorList.DropDown.Items.Add("LavenderBlush");
            colorList.DropDown.Items.Add("LawnGreen");
            colorList.DropDown.Items.Add("LemonChiffon");
            colorList.DropDown.Items.Add("LightBlue");
            colorList.DropDown.Items.Add("LightCoral");
            colorList.DropDown.Items.Add("LightCyan");
            colorList.DropDown.Items.Add("LightGoldenrodYellow");
            colorList.DropDown.Items.Add("LightGreen");
            colorList.DropDown.Items.Add("LightGray");
            colorList.DropDown.Items.Add("LightPink");
            colorList.DropDown.Items.Add("LightSalmon");
            colorList.DropDown.Items.Add("LightSeaGreen");
            colorList.DropDown.Items.Add("LightSkyBlue");
            colorList.DropDown.Items.Add("LightSlateGray");
            colorList.DropDown.Items.Add("LightSteelBlue");
            colorList.DropDown.Items.Add("LightYellow");
            colorList.DropDown.Items.Add("Lime");
            colorList.DropDown.Items.Add("LimeGreen");
            colorList.DropDown.Items.Add("Linen");
            colorList.DropDown.Items.Add("Magenta");
            colorList.DropDown.Items.Add("Maroon");
            colorList.DropDown.Items.Add("MediumAquamarine");
            colorList.DropDown.Items.Add("MediumBlue");
            colorList.DropDown.Items.Add("MediumOrchid");
            colorList.DropDown.Items.Add("MediumPurple");
            colorList.DropDown.Items.Add("MediumSeaGreen");
            colorList.DropDown.Items.Add("MediumSlateBlue");
            colorList.DropDown.Items.Add("MediumSpringGreen");
            colorList.DropDown.Items.Add("MediumTurquoise");
            colorList.DropDown.Items.Add("MediumVioletRed");
            colorList.DropDown.Items.Add("MidnightBlue");
            colorList.DropDown.Items.Add("MintCream");
            colorList.DropDown.Items.Add("MistyRose");
            colorList.DropDown.Items.Add("Moccasin");
            colorList.DropDown.Items.Add("NavajoWhite");
            colorList.DropDown.Items.Add("Navy");
            colorList.DropDown.Items.Add("OldLace");
            colorList.DropDown.Items.Add("Olive");
            colorList.DropDown.Items.Add("OliveDrab");
            colorList.DropDown.Items.Add("Orange");
            colorList.DropDown.Items.Add("OrangeRed");
            colorList.DropDown.Items.Add("Orchid");
            colorList.DropDown.Items.Add("PaleGoldenrod");
            colorList.DropDown.Items.Add("PaleGreen");
            colorList.DropDown.Items.Add("PaleTurquoise");
            colorList.DropDown.Items.Add("PaleVioletRed");
            colorList.DropDown.Items.Add("PapayaWhip");
            colorList.DropDown.Items.Add("PeachPuff");
            colorList.DropDown.Items.Add("Peru");
            colorList.DropDown.Items.Add("Pink");
            colorList.DropDown.Items.Add("Plum");
            colorList.DropDown.Items.Add("PowderBlue");
            colorList.DropDown.Items.Add("Purple");
            colorList.DropDown.Items.Add("Red");
            colorList.DropDown.Items.Add("RosyBrown");
            colorList.DropDown.Items.Add("RoyalBlue");
            colorList.DropDown.Items.Add("SaddleBrown");
            colorList.DropDown.Items.Add("Salmon");
            colorList.DropDown.Items.Add("SandyBrown");
            colorList.DropDown.Items.Add("SeaGreen");
            colorList.DropDown.Items.Add("Seashell");
            colorList.DropDown.Items.Add("Sienna");
            colorList.DropDown.Items.Add("Silver");
            colorList.DropDown.Items.Add("SkyBlue");
            colorList.DropDown.Items.Add("SlateBlue");
            colorList.DropDown.Items.Add("SlateGray");
            colorList.DropDown.Items.Add("Snow");
            colorList.DropDown.Items.Add("SpringGreen");
            colorList.DropDown.Items.Add("SteelBlue");
            colorList.DropDown.Items.Add("Tan");
            colorList.DropDown.Items.Add("Teal");
            colorList.DropDown.Items.Add("Thistle");
            colorList.DropDown.Items.Add("Tomato");
            colorList.DropDown.Items.Add("Turquoise");
            colorList.DropDown.Items.Add("Violet");
            colorList.DropDown.Items.Add("Wheat");
            colorList.DropDown.Items.Add("White");
            colorList.DropDown.Items.Add("WhiteSmoke");
            colorList.DropDown.Items.Add("Yellow");
            colorList.DropDown.Items.Add("YellowGreen");

            rtb.AcceptsTab = true;
            rtb.WordWrap = true;
            rtb.ShortcutsEnabled = false;
            rtb.ScrollBars = RichTextBoxScrollBars.Both;
            InstalledFontCollection fonts = new InstalledFontCollection();

            foreach (FontFamily family in fonts.Families)
            {
                fontList.DropDown.Items.Add(family.Name);
            }
        }

        private void newBtn_Click(object sender, EventArgs e)
        {
            rtb.Clear();
        }

        private void openBtn_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.Filter = "RTF Files (*.rtf)|*.rtf|TXT Files (*.txt)|*.txt|All files (*.*)|*.*";
            if (ofd.ShowDialog() == DialogResult.OK)
            {
                if (Path.GetExtension(ofd.FileName) == ".rtf")
                {
                    rtb.LoadFile(ofd.FileName, RichTextBoxStreamType.RichText);
                }
                else if (Path.GetExtension(ofd.FileName) == ".txt")
                {
                    rtb.LoadFile(ofd.FileName, RichTextBoxStreamType.PlainText);
                }
                else
                {
                    return;
                }
            }
            
        }

        private void saveBtn_Click(object sender, EventArgs e)
        {
            SaveFileDialog sfd = new SaveFileDialog();
            sfd.Filter = "RTF Files (*.rtf)|*.rtf|TXT Files (*.txt)|*.txt|All files (*.*)|*.*";
            if (sfd.ShowDialog() == DialogResult.OK && sfd.FileName.Length > 0)
            {
                rtb.SaveFile(sfd.FileName);
            }
        }
        private void richTextBox1_SelectionChanged(object sender, EventArgs e)
        {
            if (rtb.SelectionFont != null)
            {
                boldBtn.Checked = rtb.SelectionFont.Bold;
                italicBtn.Checked = rtb.SelectionFont.Italic;
                underlBtn.Checked = rtb.SelectionFont.Underline;
            }
        }

        private void boldBtn_Click(object sender, EventArgs e)
        {
            if (rtb.SelectionFont == null)
            {
                return;
            }

            FontStyle style = rtb.SelectionFont.Style;

            if (rtb.SelectionFont.Bold)
            {
                style &= ~FontStyle.Bold;
            }
            else
            {
                style |= FontStyle.Bold;

            }
            rtb.SelectionFont = new Font(rtb.SelectionFont, style);
        }

        private void italicBtn_Click(object sender, EventArgs e)
        {
            if (rtb.SelectionFont == null)
            {
                return;
            }
            FontStyle style = rtb.SelectionFont.Style;

            if (rtb.SelectionFont.Italic)
            {
                style &= ~FontStyle.Italic;
            }
            else
            {
                style |= FontStyle.Italic;
            }
            rtb.SelectionFont = new Font(rtb.SelectionFont, style);
        }

        private void underlBtn_Click(object sender, EventArgs e)
        {
            if (rtb.SelectionFont == null)
            {
                return;
            }

            FontStyle style = rtb.SelectionFont.Style;

            if (rtb.SelectionFont.Underline)
            {
                style &= ~FontStyle.Underline;
            }
            else
            {
                style |= FontStyle.Underline;
            }
            rtb.SelectionFont = new Font(rtb.SelectionFont, style);
        }

        private void fontList_DropDownItemClicked(object sender, ToolStripItemClickedEventArgs e)
        {
            if (rtb.SelectionFont == null)
            {
                rtb.SelectionFont = new Font(e.ClickedItem.Text, rtb.Font.Size);
            }
            rtb.SelectionFont = new Font(e.ClickedItem.Text, rtb.SelectionFont.Size);
        }
        private void colorList_DropDownItemClicked(object sender, ToolStripItemClickedEventArgs e)
        {
            KnownColor selectedColor;
            selectedColor = (KnownColor)System.Enum.Parse(typeof(KnownColor), e.ClickedItem.Text);

            rtb.SelectionColor = Color.FromKnownColor(selectedColor);
        }
        private void sizeList_DropDownItemClicked(object sender, ToolStripItemClickedEventArgs e)
        {
            if (rtb.SelectionFont == null)
            {
                return;
            }
            rtb.SelectionFont = new Font(rtb.SelectionFont.FontFamily,
                Convert.ToInt32(e.ClickedItem.Text),
                rtb.SelectionFont.Style);
        }
        private void zoomList_DropDownItemClicked(object sender, System.Windows.Forms.ToolStripItemClickedEventArgs e)
        {
            float zoomPercent = Convert.ToSingle(e.ClickedItem.Text.Trim('%'));
            rtb.ZoomFactor = zoomPercent / 100;
        }

        private void alignCenterBtn_Click(object sender, EventArgs e)
        {
            rtb.SelectionAlignment = HorizontalAlignment.Center;
        }

        private void alignLeftBtn_Click(object sender, EventArgs e)
        {
            rtb.SelectionAlignment = HorizontalAlignment.Left;
        }

        private void alignRightBtn_Click(object sender, EventArgs e)
        {
            rtb.SelectionAlignment = HorizontalAlignment.Right;
        }

        private void imageBtn_Click(object sender, EventArgs e)
        {
            /*OpenFileDialog ofd2 = new OpenFileDialog();
            ofd.Filter = "Image Files (*.gif, *.jpg, *.jpeg, *.bmp, *.wmf, *.png)|*.gif; *.jpg; *.jpeg; *.bmp; *.bmp; *.wmf; *.png|All files (*.*)|*.*";
            if (ofd.ShowDialog() == DialogResult.OK)
            {
                string EntirePath = Path.GetDirectoryName(ofd.FileName);
                string BackSlash = @"\";
                Image img = Image.FromFile(@EntirePath + BackSlash + ofd.FileName);
                Clipboard.SetImage(img);

                rtb.SelectionStart = 0;
                rtb.Paste();

                Clipboard.Clear();*/
            //}
        }

        private void printBtn_Click(object sender, EventArgs e)
        {
            PrintDialog print = new PrintDialog();
            prntDoc.PrintPage += new System.Drawing.Printing.PrintPageEventHandler(prntDoc_PrintPage);
            print.Document = prntDoc;
            if (print.ShowDialog(this) == DialogResult.OK)
            {
                prntDoc.Print();
            }
        }
        private void prntDoc_PrintPage(Object sender, System.Drawing.Printing.PrintPageEventArgs e)
        {
            Graphics g = e.Graphics;
            g.DrawString(rtb.Text, rtb.Font, Brushes.Black, 0, 0);
        }

        private void previewBtn_Click(object sender, EventArgs e)
        {
            PrintPreviewDialog preview = new PrintPreviewDialog();
            prntDoc.PrintPage += new System.Drawing.Printing.PrintPageEventHandler(prntDoc_PrintPage);
            preview.Document = prntDoc;
            if (preview.ShowDialog(this) == DialogResult.OK)
            {
                prntDoc.Print();
            }
        }


    }
}
